package gao.xiaolei.service;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import gao.xiaolei.myExcepition.MyExcepition;

import java.io.*;

@Service("FileService")
@RefreshScope
public class FileService implements InitializingBean {

    @Value("${fileConfig.defaultPath}" + "/")
    private String savePath;

    //上传文件
    public String uploadFile(MultipartFile file, String definePath, String fileUuid) throws IOException {
        return uploadFile(file.getBytes(), definePath, fileUuid);
    }

    //上传字符串当文件储存
    public String uploadFileString(String fileString, String definePath, String fileUuid) throws IOException {
        return uploadFile(fileString.getBytes(), definePath, fileUuid);
    }

    //删除文件
    public String deleteFile(String definePath, String uuid) {
        String uuidPath = makePath(uuid);
        File f = null;
        String result = "";
        if (definePath != null) {
            f = new File(savePath.concat(definePath).concat("/").concat(uuidPath));
            result.concat(definePath).concat("/").concat(uuid);
        } else {
            f = new File(savePath.concat(uuidPath));
            result.concat("/").concat(uuid);
        }
        if (f.exists())
            f.delete();
        return result;
    }

    //获取文件
    public byte[] getFile(String definePath, String uuid) throws MyExcepition, IOException {
        String uuidPath = makePath(uuid);
        byte[] fileContent = null;
        File f = null;
        if (definePath != null)
            f = new File(savePath.concat(definePath).concat("/").concat(uuidPath));
        else
            f = new File(savePath.concat(uuidPath));
        if (!f.exists()) {
            throw new MyExcepition("文件不存在!");
        } else {// 将文件二进制数据返回
            InputStream fis = new FileInputStream(f);
            fileContent = FileCopyUtils.copyToByteArray(fis);
        }
        return fileContent;
    }

    //复制文件
    public void copyFile(String oldDefinePath, String oldUuid, String newDefinePath, String newUuid) throws MyExcepition, IOException {
        String oldUuidPath = makePath(oldUuid);
        File oldf = null;
        if (oldDefinePath != null)
            oldf = new File(savePath.concat(oldDefinePath).concat("/").concat(oldUuidPath));
        else
            oldf = new File(savePath.concat(oldUuidPath));
        if (!oldf.exists())
            throw new MyExcepition("文件不存在!");
        else {
            String newUuidPath = makePath(newUuid);
            File newf = null;
            if (newDefinePath != null) {
                newf = new File(savePath.concat(newDefinePath));
                if (!newf.exists())// 如果文件夹不存在
                    newf.mkdir();// 创建文件夹
                newf = new File(savePath.concat(newDefinePath).concat("/").concat(newUuid.substring(0, 2)));
                if (!newf.exists())// 如果文件夹不存在
                    newf.mkdir();// 创建文件夹
                newf = new File(savePath.concat(newDefinePath).concat("/").concat(newUuidPath));
            } else {
                newf = new File(savePath.concat(newUuid.substring(0, 2)));
                if (!newf.exists())// 如果文件夹不存在
                    newf.mkdir();// 创建文件夹
                newf = new File(savePath.concat(newUuidPath));
            }
            FileCopyUtils.copy(oldf, newf);
        }
    }

    private String makePath(String uuid) {
        return uuid.substring(0, 2).concat("/").concat(uuid.substring(2, 32));
    }

    private String uploadFile(byte[] fileByte, String definePath, String fileUuid) {
        String uuidPath = makePath(fileUuid);//将uuid改成路径形式
        String realPath;//文件最后用来保存的路径
        String twoFront = fileUuid.substring(0, 2);
        if (definePath != null) {//如果指定了对应的储存文件夹则储存到对应的文件夹下
            File fileDefinePath = new File(savePath.concat(definePath));
            if (!fileDefinePath.exists())// 如果文件夹不存在
                fileDefinePath.mkdir();// 创建文件夹
            fileDefinePath = new File(savePath + definePath.concat("/").concat(twoFront));
            if (!fileDefinePath.exists())// 如果文件夹不存在
                fileDefinePath.mkdir();// 创建文件夹
            realPath = definePath.concat("/");
        } else {
            // 判断文件夹下有没有文件UUID前两位对应的目录,如果没有则创建
            File filePath = new File(savePath + twoFront);
            if (!filePath.exists())// 如果文件夹不存在
                filePath.mkdir();// 创建文件夹
            realPath = "";
        }
        // 将路径和文件名进行组合构建File对象
        String resultPath = savePath.concat(realPath).concat(uuidPath);
        File fileToSave = new File(resultPath);
        // 通过Spring的文件复制函数将二进制格式的文件复制到对应的路径下
        try {
            FileCopyUtils.copy(fileByte, fileToSave);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 返回文件储存路径
        return resultPath;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        File filePath = new File(savePath);
        if (!filePath.exists())// 如果文件夹不存在
            filePath.mkdir();// 创建文件夹
    }
}
